package package01.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * 作者: jack
 * 时间: 2020-07-20 09:16
 * 描述: DBUtil
 */
public class DBUtil {

    private Connection connection;
    private String driverClassName = "oracle.jdbc.OracleDriver";
    private String url = "jdbc:oracle:thin:@39.105.173.35:1521:helowin";
    private String username = "scott";
    private String password = "123456";

    /**
     * 连接数据库
     *
     * @return
     */
    public Connection init() {
        try {
            // 加载驱动
            Class.forName(driverClassName);
            // 获取会话 Connection
            connection = DriverManager.getConnection(url, username, password);
            // 数据库连接成功
//            System.out.println("数据库连接成功...");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return connection;
    }

    /**
     * 关闭数据库
     */
    public void close() {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
